package com.ZeroCarbon.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import com.ZeroCarbon.domain.po.Good;
import com.ZeroCarbon.utils.BusinessMethodUtils;
import com.ZeroCarbon.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.ZeroCarbon.prefix.FilePrefix.GOOD_IMAGES;

/**
 * 商品详细类
 *
 * @since 2024-11-10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GoodsReceiverDTO {
    /**
     * 商品id
     */
    private String id;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 身份id
     */
    private String identityId;

    /**
     * 名称
     */
    private String name;

    /**
     * 图片
     */
    private List<MultipartFile> images;

    /**
     * 描述
     */
    private String description;

    /**
     * 价格
     */
    private double price;

    /**
     * 所需积分
     */
    private int integral;

    /**
     * 标签Json
     */
    private List<String> tags;

    /**
     * 库存
     */
    private int number;

    /**
     * GoodsReceiverDTO转PO
     */
    public Good toGood() {
        //拷贝对象
        Good good = BeanUtil.copyProperties(this, Good.class);
        //上传文件生成列表
        List<String> filesList = BusinessMethodUtils.generateFilesList(GOOD_IMAGES, images);
        //设置额外信息
        return good.setImagesJson(JsonUtils.ObjectToJson(filesList))
                .setTagsJson(JsonUtils.ObjectToJson(tags));
    }
}
